package com.fare.taluo.application.impl.analyse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.filter.Filter;
import org.foreveross.hbase.client.HBaseHelper;
import org.foreveross.hbase.client.HBasePlaneinfoHelper;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.vo.core.PlaneInfo;
import com.fare.taluo.application.analyse.HistoryApplicaiton;

@Transactional
@Scope("singleton")
@Service("historyApplicaiton")
public class HistoryApplicaitonImpl implements HistoryApplicaiton {

	public List<PlaneInfo> searchHistoryFeight(String tableName,String startRowKey,String endRowKey,List<Filter> filters) throws IOException {
		List<PlaneInfo> list=new ArrayList<PlaneInfo> ();
		//航班最低价LP_FLIGHT：  渠道_起点(使用三字码)_终点(使用三字码)_(航班号)_航班日期(年月日时)
		//LP_LINE  渠道_起点(使用三字码)_终点(使用三字码)_航班日期(年月日时)

		ResultScanner scan=HBaseHelper.getRecords(tableName, startRowKey, endRowKey,null,filters);
		list=HBasePlaneinfoHelper.transformHBaseResultToPlaneinfo(scan);
		return list;

	}
	
	public List<PlaneInfo> getPlaneInfos(List<String> dates,String rowKeyHead,ChoseEnum choseEnum)throws Exception{
		//单选
		return choseEnum.getPlaneInfos(dates, rowKeyHead);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<PlaneInfo> getPlaneInfos(List<String> dates,String rowKeyHead,String str)throws Exception{
	if(str.length()==1){//单选
		return this.getPlaneInfos(dates, rowKeyHead, ChoseEnum.valueOf(str));
	}else{//组合
		List<List<PlaneInfo>> lists=new ArrayList();
		for (int i = 0; i < str.length(); i++) {
			lists.add(this.getPlaneInfos(dates, rowKeyHead, ChoseEnum.valueOf(str.charAt(i)+"")));
		}
		List<PlaneInfo> listInfo=new ArrayList();
		for (Iterator iterator = lists.iterator(); iterator.hasNext();) {
			List<PlaneInfo> list = (List<PlaneInfo>) iterator.next();
			for (Iterator iterator2 = list.iterator(); iterator2.hasNext();) {
				PlaneInfo planeInfo = (PlaneInfo) iterator2.next();
				if(planeInfo!=null){
					listInfo.add(planeInfo);
				}
			}
		}
		return listInfo;
	}
}
	
    //查航班的准确到小时的信息
	public PlaneInfo searchOnePlaneInfo(String tableName,String startRowKey, String endRowKey) throws IOException {
		List<PlaneInfo> list = new ArrayList<PlaneInfo>();
		// 航班最低价LP_FLIGHT： 渠道_起点(使用三字码)_终点(使用三字码)_(航班号)_航班日期(年月日时)
		// LP_LINE 渠道_起点(使用三字码)_终点(使用三字码)_航班日期(年月日时)
		ResultScanner scan = HBaseHelper.getRecords(tableName, startRowKey,endRowKey);
		list = HBasePlaneinfoHelper.transformHBaseResultToPlaneinfo(scan);
		if(list.size()== 0){
			return null;
		}
		return list.get(0);

	}
	public static void main(String[] args) throws IOException {
		HistoryApplicaitonImpl imp = new HistoryApplicaitonImpl();
		String startRowKey="00001CANPEK00CZ31112013031400";
		String   endRowKey="00001CANPEK00CZ31112013031423";

		List<PlaneInfo>  ko= imp.searchHistoryFeight("LP_FLIGHT", startRowKey, endRowKey,null);
	      
		System.out.println(ko.size());
		
		
		
		//String startRowKey="20005CANSHA00CZ35232013010708";
		//String   endRowKey="20005CANSHA00CZ35472013021019";
		//String startRowKey="00002CANSHA00CZ63222013010100";
		//String   endRowKey="00002CANSHA00CZ63222013021901";
		/*String startRowKey="20005CANXMN00CZ59882013010108";
		String   endRowKey="20005CANXMN00CZ59882013011208";*/
		/*PlaneInfo info = imp.searchOnePlaneInfo("LP_FLIGHT", startRowKey, endRowKey);
	       System.out.println(info.getCarrierName());
	       System.out.println(info.getLowprPrice());
	       System.out.println(info.getFlightBatchStartTime());*/
		/*String startRowKey="20005CANXMN201301011z";
		String   endRowKey="20005CANXMN2013041208";
		List<PlaneInfo> list = imp.searchHistoryFeight("LP_LINE",startRowKey, endRowKey);
		System.out.println(list.size());
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			PlaneInfo planeInfo = (PlaneInfo) iterator.next();
			System.out.print(planeInfo.getLowprPrice() +"@@");
			System.out.println(planeInfo.getFlightBatchStartTime()+"@@"+planeInfo.getFlightBatchDate());
		}*/
		/*List<PlaneInfo> list = imp.searchHistoryFeight("LP_FLIGHT",startRowKey, endRowKey);
		System.out.println(list.size());
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			PlaneInfo planeInfo = (PlaneInfo) iterator.next();
			System.out.print(planeInfo.getLowprPrice() +"@@");
			System.out.println(planeInfo.getFlightBatchStartTime()+"@@"+planeInfo.getFlightBatchDate());
		}*/
		/*for (int i = 0; i < 10; i++) {
			
		}*/
       
	}

}
